<?php
/**
 * Created by PhpStorm.
 * User: glq
 * Date: 2019/4/4
 * Time: 13:30
 */
namespace Portal\Controller;

use Common\Controller\AdminbaseController;
use Portal\Model\PostsModel;
use Label\Model\CategoryModel;

// 快讯
class AdminsubjectController extends AdminbaseController {

    public function subject_lists()
    {
        $this->_lists();
        $this->display();
    }

    public function edit_subject()
    {
        $id = I('get.id');
        if (IS_POST) {
            $_POST['subject']['pc_img']     = sp_asset_relative_url($_POST['subject']['pc_img']);
            $_POST['subject']['mobile_img'] = sp_asset_relative_url($_POST['subject']['mobile_img']);
            $_POST['subject']['icon']       = sp_asset_relative_url($_POST['subject']['icon']);
            $_POST['subject']['surface_img']= sp_asset_relative_url($_POST['subject']['surface_img']);

            $categorys = $_POST['subject']['categorys'];
            if (empty($categorys)) {
                $this->error("请至少选择一个分类！");
            }
            $_POST['subject']['categorys'] = implode(',', $categorys);

            $subject    = I("post.subject");
            $id         = $id ?: $subject['id'];
            if ($id) {
                M('subject')->where(['id' => $id])->save($subject);
            } else {
                unset($subject['id']);
                $subject['updated_at'] = time();
                $subject['created_at'] = time();
                $result = M('subject')->add($subject);

                // 主动提交百度seo链接
                if (C('PUSH_SEO_URL')) {
                    $urls = array(C('HTTP_METHOD') . C('DOMAIN_NAME') . U('Portal/Blueprint/index', ['id' => $result]));
                    post_baidu_seo_url($urls);
                }
            }
            $this->success('操作成功');
        } else {
            if ($id) {
                $subject = M('subject')->where(['id' => $id])->find();
                if ($subject) {
                    $del = I('get.del');
                    if ($del) {
                        M('subject')->where(['id' => $id])->delete();
                        $this->success('操作成功');exit();
                    }
                    $this->assign('subject', $subject);
                }
            }
            $subject_categorys = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_SUBJECT, 'status' => 1])->order('id asc')->select();
            $this->assign('subject_categorys', $subject_categorys);
            $this->display();
        }
    }

    // 编辑专题相关课程专题
    public function edit_lesson_subject()
    {
        $subject_id = I('get.subject_id');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $this->assign('subject_id', $subject_id);
                $this->assign('subject', $subject);

                $where      = array();
                $keyword    = I('request.keyword');
                if(!empty($keyword)){
                    $where['subject_name'] = array('like', "%$keyword%");
                }
                $count  = M('lesson_subject')->where($where)->count();
                $page   = $this->page($count, 10);
                $lists  = M('lesson_subject')->where($where)->order('sort desc, id desc')->limit($page->firstRow , $page->listRows)->select();

                $this->assign("page", $page->show('Admin'));
                $this->assign('lists', $lists);

                $this->display();
            }
        }
    }

    // 增加/删除 关联的课程专题
    public function change_subject_lesson_subject()
    {
        $subject_id         = I('get.subject_id');
        $lesson_subject_id  = I('get.lesson_subject_id');
        $type               = I('get.type');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $lesson_subject_ids = explode(',', $subject['lesson_subject_ids']);
                if ($type == 'del') {
                    if (in_array($lesson_subject_id, $lesson_subject_ids)) {
                        unset($lesson_subject_ids[array_search($lesson_subject_id, $lesson_subject_ids)]);
                        $subject['lesson_subject_ids'] = implode(',', array_filter($lesson_subject_ids));
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                } elseif ($type == 'add') {
                    if (!in_array($lesson_subject_id, $lesson_subject_ids)) {
                        $lesson_subject_ids[] = $lesson_subject_id;
                        sort($lesson_subject_ids);
                        $subject['lesson_subject_ids'] = implode(',', $lesson_subject_ids);
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                }
                $this->success('操作成功');
            }
        }
    }

    // 编辑专题相关文章
    public function edit_article()
    {
        $subject_id = I('get.subject_id');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $this->assign('subject_id', $subject_id);
                $this->assign('subject', $subject);

                $where      = array('post_type' => PostsModel::POST_TYPE_ARTICLE, 'post_status' => PostsModel::POST_STATUS_NORMAL);
                $keyword    = I('request.keyword');
                if(!empty($keyword)){
                    $where['post_title'] = array('like', "%$keyword%");
                }
                $count  = M('posts')->where($where)->count();
                $page   = $this->page($count, 10);
                $lists  = M('posts')->where($where)->order('post_order desc, id desc')->limit($page->firstRow , $page->listRows)->select();

                $this->assign("page", $page->show('Admin'));
                $this->assign('lists', $lists);

                $this->display();
            }
        }
    }

    // 增加/删除 关联的文章
    public function change_subject_article()
    {
        $subject_id     = I('get.subject_id');
        $article_id     = I('get.article_id');
        $type           = I('get.type');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $article_ids = explode(',', $subject['article_ids']);
                if ($type == 'del') {
                    if (in_array($article_id, $article_ids)) {
                        unset($article_ids[array_search($article_id, $article_ids)]);
                        $subject['article_ids'] = implode(',', array_filter($article_ids));
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                } elseif ($type == 'add') {
                    if (!in_array($article_id, $article_ids)) {
                        $article_ids[] = $article_id;
                        sort($article_ids);
                        $subject['article_ids'] = implode(',', $article_ids);
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                }
                $this->success('操作成功');
            }
        }
    }

    // 编辑专题相关商脉（服务商）
    public function edit_service()
    {
        $subject_id = I('get.subject_id');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $this->assign('subject_id', $subject_id);
                $this->assign('subject', $subject);

                $where      = array('status' => 1, 'del_time' => 0);
                $keyword    = I('request.keyword');
                if(!empty($keyword)){
                    $where['company_name'] = array('like', "%$keyword%");
                }
                $count  = M('service_provider')->where($where)->count();
                $page   = $this->page($count, 10);
                $lists  = M('service_provider')->where($where)->order('sort desc, id desc')->limit($page->firstRow , $page->listRows)->select();

                $this->assign("page", $page->show('Admin'));
                $this->assign('lists', $lists);

                $this->display();
            }
        }
    }

    // 增加/删除 关联的商脉（服务商）
    public function change_subject_service()
    {
        $subject_id         = I('get.subject_id');
        $service_id         = I('get.service_id');
        $type               = I('get.type');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $service_ids = explode(',', $subject['service_ids']);
                if ($type == 'del') {
                    if (in_array($service_id, $service_ids)) {
                        unset($service_ids[array_search($service_id, $service_ids)]);
                        $subject['service_ids'] = implode(',', array_filter($service_ids));
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                } elseif ($type == 'add') {
                    if (!in_array($service_id, $service_ids)) {
                        $service_ids[] = $service_id;
                        sort($service_ids);
                        $subject['service_ids'] = implode(',', $service_ids);
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                }
                $this->success('操作成功');
            }
        }
    }


    // 编辑专题相关活动
    public function edit_activity()
    {
        $subject_id = I('get.subject_id');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $this->assign('subject_id', $subject_id);
                $this->assign('subject', $subject);

                $where      = array('status' => 1);
                $keyword    = I('request.keyword');
                if(!empty($keyword)){
                    $where['name'] = array('like', "%$keyword%");
                }
                $count  = M('activity')->where($where)->count();
                $page   = $this->page($count, 10);
                $lists  = M('activity')->where($where)->order('sort desc, id desc')->limit($page->firstRow , $page->listRows)->select();

                $this->assign("page", $page->show('Admin'));
                $this->assign('lists', $lists);

                $this->display();
            }
        }
    }

    // 增加/删除 关联的活动
    public function change_subject_activity()
    {
        $subject_id         = I('get.subject_id');
        $activity_id        = I('get.activity_id');
        $type               = I('get.type');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $activity_ids = explode(',', $subject['activity_ids']);
                if ($type == 'del') {
                    if (in_array($activity_id, $activity_ids)) {
                        unset($activity_ids[array_search($activity_id, $activity_ids)]);
                        $subject['activity_ids'] = implode(',', array_filter($activity_ids));
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                } elseif ($type == 'add') {
                    if (!in_array($activity_id, $activity_ids)) {
                        $activity_ids[] = $activity_id;
                        sort($activity_ids);
                        $subject['activity_ids'] = implode(',', $activity_ids);
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                }
                $this->success('操作成功');
            }
        }
    }

    // 编辑专题相关案例
    public function edit_case()
    {
        $subject_id = I('get.subject_id');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $this->assign('subject_id', $subject_id);
                $this->assign('subject', $subject);

                $where      = array('post_type' => PostsModel::POST_TYPE_ARTICLE, 'post_status' => PostsModel::POST_STATUS_NORMAL);
                $keyword    = I('request.keyword');
                if(!empty($keyword)){
                    $where['post_title'] = array('like', "%$keyword%");
                }
                $count  = M('posts')->where($where)->count();
                $page   = $this->page($count, 10);
                $lists  = M('posts')->where($where)->order('post_order desc, id desc')->limit($page->firstRow , $page->listRows)->select();

                $this->assign("page", $page->show('Admin'));
                $this->assign('lists', $lists);

                $this->display();
            }
        }
    }

    // 增加/删除 关联的案例
    public function change_subject_case()
    {
        $subject_id     = I('get.subject_id');
        $case_id        = I('get.case_id');
        $type           = I('get.type');
        if ($subject_id) {
            $subject = M('subject')->where(['id' => $subject_id])->find();
            if ($subject) {
                $case_ids = explode(',', $subject['case_ids']);
                if ($type == 'del') {
                    if (in_array($case_id, $case_ids)) {
                        unset($case_ids[array_search($case_id, $case_ids)]);
                        $subject['case_ids'] = implode(',', array_filter($case_ids));
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                } elseif ($type == 'add') {
                    if (!in_array($case_id, $case_ids)) {
                        $case_ids[] = $case_id;
                        sort($case_ids);
                        $subject['case_ids'] = implode(',', $case_ids);
                        M('subject')->where(['id' => $subject_id])->save($subject);
                    }
                }
                $this->success('操作成功');
            }
        }
    }

    private function _lists($where = array()) {

        $keyword = I('request.keyword');
        if(!empty($keyword)){
            $where['name'] = array('like', "%$keyword%");
        }
        $model  = M('subject');
        $count  = $model->where($where)->count();
        $page   = $this->page($count, 20);

        $lists  = $model
            ->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->order("sort desc, created_at desc")
            ->select();

        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET, $_POST));
        $this->assign("lists", $lists);
    }
}